/*

BlackRabbit - OpenSource IDE.
@author Edward Sarkisyan (edward.sarkisyan@gmail.com)
Use LGPL (http://www.gnu.org/copyleft/lesser.html)

*/

#include "newprojectdialog.h"
#include "ui_newprojectdialog.h"
#include <QXmlStreamWriter>
#include <QFileDialog>
#include <QMessageBox>
#include <QDateTime>
#include "../globals.h"
#include "../languageextentioninterface.h"
#include "../classes/languagedata.h"

NewProjectDialog::NewProjectDialog(QWidget *parent) :
    QDialog(parent),
    m_ui(new Ui::NewProjectDialog)
{
    m_ui->setupUi(this);
    for(QVector<Language*>::const_iterator i = languages->begin(); i!= languages->end();i++)
    {
        m_ui->languageComboBox->addItem((*i)->name(), LanguageData((*i)));
    }
}

NewProjectDialog::~NewProjectDialog()
{
    delete m_ui;
}

void NewProjectDialog::changeEvent(QEvent *e)
{
    QDialog::changeEvent(e);
    switch (e->type()) {
    case QEvent::LanguageChange:
        m_ui->retranslateUi(this);
        break;
    default:
        break;
    }
}

void NewProjectDialog::on_projectNameEdit_textEdited(QString projectName)
{
    if(projectName.size() > 10)
        m_ui->projectNameEdit->setStyleSheet("color: red");
    else
        m_ui->projectNameEdit->setStyleSheet("color: green");
}

void NewProjectDialog::on_localProjectPath_textEdited(QString projectName)
{
    if(projectName.size() > 10)
        m_ui->projectNameEdit->setStyleSheet("color: red");
    else
        m_ui->projectNameEdit->setStyleSheet("color: green");
}



void NewProjectDialog::on_selectLocalFolderToolBox_clicked()
{
    m_ui->localProjectPath->setText(QFileDialog::getExistingDirectory(this, tr("Select folder")));
}

void NewProjectDialog::on_useCSV_stateChanged(int state)
{
    if(state != Qt::Checked)
    {
        m_ui->CSVComboBox->setEnabled(false);
        m_ui->CSVServerEdit->setEnabled(false);
        m_ui->CSVServerLabel->setEnabled(false);
        m_ui->CSVAutenticationCheckBox->setEnabled(false);
        m_ui->CSVPassword->setEnabled(false);
        m_ui->CSVPasswordLabel->setEnabled(false);
        m_ui->CSVUsername->setEnabled(false);
        m_ui->CSVUsernameLabel->setEnabled(false);

    }
    else
    {
        m_ui->CSVComboBox->setEnabled(true);
        m_ui->CSVServerEdit->setEnabled(true);
        m_ui->CSVServerLabel->setEnabled(true);
        m_ui->CSVAutenticationCheckBox->setEnabled(true);
        m_ui->CSVPassword->setEnabled(true);
        m_ui->CSVPasswordLabel->setEnabled(true);
        m_ui->CSVUsername->setEnabled(true);
        m_ui->CSVUsernameLabel->setEnabled(true);
    }
}

void NewProjectDialog::on_CSVAutenticationCheckBox_stateChanged(int state)
{
    if( state != Qt::Checked)
    {
        m_ui->CSVPassword->setEnabled(true);
        m_ui->CSVPasswordLabel->setEnabled(true);
        m_ui->CSVUsername->setEnabled(true);
        m_ui->CSVUsernameLabel->setEnabled(true);
    }
    else
    {
        m_ui->CSVPassword->setEnabled(false);
        m_ui->CSVPasswordLabel->setEnabled(false);
        m_ui->CSVUsername->setEnabled(false);
        m_ui->CSVUsernameLabel->setEnabled(false);
    }
}

void NewProjectDialog::on_buttonBox_accepted()
{
    QString projectFilePath = m_ui->localProjectPath->text()
                              +"/"
                              +m_ui->projectNameEdit->text();
    if(QFile::exists(projectFilePath))
    {
        if (QMessageBox::question(this, tr("Question"),
                              QString(tr("File %1 is exists. Do you want overwrite it?")).arg(projectFilePath),
                              (QMessageBox::Yes | QMessageBox::No)) != QMessageBox::Yes)
            return;
    }
    QFile projectFile(projectFilePath);
    projectFile.open(QFile::WriteOnly);
    QXmlStreamWriter xml(&projectFile);
    xml.setAutoFormatting(true);
    xml.writeStartDocument();
        xml.writeStartElement("project");
            xml.writeTextElement("name", m_ui->projectNameEdit->text());
            xml.writeTextElement("language", m_ui->languageComboBox->currentText());
            xml.writeTextElement("author", "Edward_PREUSER");
            xml.writeTextElement("created", QDateTime::currentDateTime().toString());
            xml.writeTextElement("description", "");
            xml.writeTextElement("CSV-SERVER", m_ui->CSVServerEdit->text());
            xml.writeTextElement("version","0.0.0");
                xml.writeStartElement("files");
                    xml.writeStartElement("folder");
                        xml.writeAttribute("name", "src");
                        xml.writeTextElement("file", "main.cpp");
                    xml.writeEndElement();
                    xml.writeStartElement("folder");
                        xml.writeAttribute("name", "doc");
                        xml.writeTextElement("file", "index.html");
                    xml.writeEndElement();
                xml.writeEndElement();
        xml.writeEndElement();
    xml.writeEndDocument();
    projectFile.close();
    this->close();
    m_projectFileName = projectFilePath;
}

QString NewProjectDialog::getProjectFileName()
{
    return m_projectFileName;
}
